################################################################################
#
# Software Detection
#
################################################################################

import pyflag.DB as DB
import pyflag.Reports as Reports
import pyflag.conf
from pyflag.ColumnTypes import StringType, IntegerType
from PseudoStat import pseudo_stat_calculate
from PartSum import part_sum_calculate
config = pyflag.conf.ConfObject()



class SoftwareDetector(Reports.report):
    name = 'Browser software'
    family = 'Software'
    description = 'Browse result software detection'
    order = 20


    def form(self, query, result):
        result.case_selector()


    def display(self, query, result):
        result.heading("Software browser")
        dbh = DB.DBO(query['case'])
        main_result = result


        def PartSumView(query, result):
            try:
                result.table(elements = [
                    StringType(name = "Program", column = "ProductName"),
                    StringType(name = "Version", column = "ProductVersion"),
                    IntegerType(name = "Count", column = "Count"),
                    IntegerType(name = "NSRL Count", column = "NSRLCount"),
                    StringType(name = "Normal", column = "NormalMembership"),
                    StringType(name = "Extra", column = "ExtraMembership"),
                    StringType(name = "Ultra", column = "UltraMembership"),
		    StringType(name = "LIN5", column = "LIN_5"),
		    StringType(name = "LIN10", column = "LIN_10")
                    ],
                table = config.PART_SUM_RESULT_TABLE,
                where = 'NormalMembership >= 0.1',
		case = query['case'],
                )
            except DB.DBError, e:
                result.text("Scan filesystem with HASH SCANNER or read NSRL db.\n%s" % e, style = 'red')


        def PseudoStatView(query, result):
            try:
                result.table(elements = [
                    StringType(name = "Program", column = "ProductName"),
                    StringType(name = "Version", column = "ProductVersion"),
                    IntegerType(name = "Count", column = "Count"),
                    IntegerType(name = "NSRLCount", column = "NSRLCount"),
                    IntegerType(name = "Result", column = "Result")],
                table = config.PS_RESULT_TABLE,
                where = 'Result >= 0.1',
		case = query['case'],
                )
            except DB.DBError, e:
                result.text("Scan filesystem with HASH SCANNER or read NSRL db.\n%s" % e, style = 'red')



        result.notebook(
            names = ["PartSum", "PseudoStat"],
            callbacks = [PartSumView, PseudoStatView],
            )


    def analyse(self, query):
	try:
    	    part_sum_calculate(case = query['case'])
	    pseudo_stat_calculate(case = query['case'])
	except:
	    pass

    def progress(self, query, result):
        result.heading("Please wait...")
        result.text("Software detection. This may take several minutes.")
